using UnityEngine;
using System.Collections;

public class Enemy : Character {

    public float movementSpeed = 1.0f;
    public GameObject player;
    public bool enemyActive = true;
    public float verticalStopDistance = 1.0f;
    public float horizontalStopDistance = 2.0f;
    public float targetSeparation = 1.0f;
    public float totalAttackCooldown = .5f;
    public float attackCooldownTimer = 0.0f;
	
	// animation-related variables
	public float idleAnimationSpeed = 0.18f;
	public float runningAnimationSpeed = 0.2f;
    public float attackingAnimationSpeed = 0.1f;
    public float dyingAnimationSpeed = 0.18f;
    protected bool isDead;
	
	// Use this for initialization
    void Start()
    {
        InitializeAnimationList();
        AddAnimation("running", 0, 2, 3, runningAnimationSpeed);
        AddAnimation("attacking", 2, 0, 3, attackingAnimationSpeed);
        AddAnimation("idle", 0, 2, 3, idleAnimationSpeed);
        AddAnimation("dying", 1, 0, 3, dyingAnimationSpeed);

        StartAnimation("idle", true);

        //Ugh, inheritance problems, copying damagable::start...
        currentHP = totalHP;
	}
	
	// Update is called once per frame
	protected override void Update () {
        if (enemyActive && !isAttacking && player && !isDead)
        {
            //Chase the player!
            Vector2 displacement = Vector2.zero;
            Vector2 this2D = new Vector2(gameObject.transform.position.x, gameObject.transform.position.z);
            Vector2 player2D = new Vector2(player.transform.position.x, player.transform.position.z);
            displacement.x = this2D.x - player2D.x;
            displacement.y = this2D.y - player2D.y;

            bool standingStill = false;

            Vector2 targetPos = Vector2.zero;
            
            //Close and on right
            if (displacement.x > 0 && displacement.x < horizontalStopDistance && Mathf.Abs(displacement.y)<verticalStopDistance)
            {
                standingStill = true;
                SetFacing(Facing.Left);
            }
            //Close and on left
            else if (displacement.x < 0 && displacement.x > -1*horizontalStopDistance && Mathf.Abs(displacement.y) < verticalStopDistance)
            {
                standingStill = true;
                SetFacing(Facing.Right);
            }
            //We're on the right but not close
            else if (displacement.x > 0)
            {
                targetPos = player2D;
                targetPos.x += horizontalStopDistance;
            }
            //We're on the left but not close
            else
            {
                targetPos = player2D;
                targetPos.x -= horizontalStopDistance;
            }

            if (!standingStill)
            {
                //Move to our new position
                Vector2 directionToTravel = targetPos - this2D;
                directionToTravel.Normalize();
                directionToTravel *= movementSpeed * Time.deltaTime;
                MoveCharacter(new Vector3(directionToTravel.x, 0.0f, directionToTravel.y));
                if (GetCurrentAnimationName() != "running")
                    StartAnimation("running", true);
                //Reset every frame..
                attackCooldownTimer = totalAttackCooldown;
            }
            else
            {
                //Let's attack randomly
                attackCooldownTimer -= Time.deltaTime;
                if (attackCooldownTimer <= 0.0f)
                {
                    Attack(0);
                    if (GetCurrentAnimationName() != "attacking")
                        StartAnimation("attacking", false);
                }
                else
                {
                    if (GetCurrentAnimationName() != "idle")
                        StartAnimation("idle", false);
                }
            }
        }

        base.Update();
	}

    public override void OnAttackComplete()
    {
        base.OnAttackComplete();
    }

    protected override void OnAnimationFinished(string name)
    {
        if (name == "dying")
        {
            base.Die();
        }
        if (name == "attacking")
        {
            attackCooldownTimer = totalAttackCooldown;
        }
    }
	
	protected override void Die()
    {
        collider.enabled = false;
		SoundEffects.instance.playEnemyDeath();
        StartAnimation("dying", false);
        isDead = true;
    }
	
	// Update for butter enemy
	protected void ParentUpdate() {
		base.Update();
	}
}
